home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gximage5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-19  |  7.1 KB  |  244 lines

  1. /* Copyright (C) 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gximage5.c */
  20. /* Interpolated image procedures */
  21. #include "gx.h"
  22. #include "math_.h"
  23. #include "memory_.h"
  24. #include "gpcheck.h"
  25. #include "gserrors.h"
  26. #include "gxfixed.h"
  27. #include "gxfrac.h"
  28. #include "gxarith.h"
  29. #include "gxmatrix.h"
  30. #include "gsccolor.h"
  31. #include "gspaint.h"
  32. #include "gxdevice.h"
  33. #include "gxcmap.h"
  34. #include "gxdcolor.h"
  35. #include "gxistate.h"
  36. #include "gzpath.h"
  37. #include "gxdevmem.h"
  38. #include "gxcpath.h"
  39. #include "gximage.h"
  40.  
  41. /* ------ Strategy procedure ------ */
  42.  
  43. /* If we're interpolating, use special logic. */
  44. private irender_proc(image_render_interpolate);
  45. private irender_proc_t
  46. image_strategy_interpolate(gx_image_enum *penum)
  47. {    const gs_imager_state *pis = penum->pis;
  48.     gs_memory_t *mem = penum->memory;
  49.     stream_IScale_state iss;
  50.     stream_IScale_state *pss;
  51.     byte *line;
  52.     const gs_color_space *pcs = penum->pcs;
  53.     gs_point dst_xy;
  54.  
  55.     if ( !penum->interpolate )
  56.       return 0;
  57.     if ( penum->posture != image_portrait || penum->masked
  58. #ifdef DPNEXT
  59.          || penum->has_alpha
  60. #endif
  61.        )
  62.       {    /* We can't handle these cases yet.  Punt. */
  63.         penum->interpolate = false;
  64.         return 0;
  65.       }
  66.     iss.memory = mem;
  67.     /* Non-ANSI compilers require the following casts: */
  68.     gs_distance_transform((float)penum->rect.w, (float)penum->rect.h,
  69.                   &penum->matrix, &dst_xy);
  70.     if ( penum->bps <= 8 && penum->device_color )
  71.       iss.BitsPerComponentIn = 8,
  72.       iss.MaxValueIn = 0xff;
  73.     else
  74.       iss.BitsPerComponentIn = sizeof(frac) * 8,
  75.       iss.MaxValueIn = frac_1;
  76.     iss.BitsPerComponentOut = sizeof(frac) * 8;
  77.     iss.MaxValueOut = frac_1;
  78.     iss.WidthOut = (int)ceil(fabs(dst_xy.x));
  79.     iss.HeightOut = (int)ceil(fabs(dst_xy.y));
  80.     iss.WidthIn = penum->rect.w;
  81.     iss.HeightIn = penum->rect.h;
  82.     iss.Colors = cs_concrete_space(pcs, pis)->type->num_components;
  83.     /* Allocate a buffer for one source/destination line. */
  84.     { uint in_size =
  85.         iss.WidthIn * iss.Colors * (iss.BitsPerComponentIn / 8);
  86.       uint out_size =
  87.         iss.WidthOut * iss.Colors *
  88.           max(iss.BitsPerComponentOut / 8, sizeof(gx_color_index));
  89.       line = gs_alloc_bytes(mem, max(in_size, out_size),
  90.                 "image scale src line");
  91.     }
  92.     pss = gs_alloc_struct(mem, stream_IScale_state,
  93.                   &st_IScale_state, "image scale state");
  94.     if ( line == 0 || pss == 0 ||
  95.          (*pss = iss,
  96.           (*s_IScale_template.init)((stream_state *)pss) < 0)
  97.        )
  98.       { gs_free_object(mem, pss, "image scale state");
  99.         gs_free_object(mem, line, "image scale src line");
  100.         /* Try again without interpolation. */
  101.         penum->interpolate = false;
  102.         return 0;
  103.       }
  104.     penum->line = line;
  105.     penum->scaler = pss;
  106.     penum->line_xy = 0;
  107.     penum->xyi.x = fixed2int_pixround(dda_current(penum->dda.pixel0.x));
  108.     penum->xyi.y = fixed2int_pixround(dda_current(penum->dda.pixel0.y));
  109.     if_debug0('b', "[b]render=interpolate\n");
  110.     return image_render_interpolate;
  111. }
  112.  
  113. void
  114. gs_gximage5_init(gs_memory_t *mem)
  115. {    image_strategies.interpolate = image_strategy_interpolate;
  116. }
  117.  
  118. /* ------ Rendering for interpolated images ------ */
  119.  
  120. private int
  121. image_render_interpolate(gx_image_enum *penum, const byte *buffer,
  122.   int data_x, uint iw, int h, gx_device *dev)
  123. {    stream_IScale_state *pss = penum->scaler;
  124.     const gs_imager_state *pis = penum->pis;
  125.     const gs_color_space *pcs = penum->pcs;
  126.     gs_logical_operation_t lop = penum->log_op;
  127.     int c = pss->Colors;
  128.     stream_cursor_read r;
  129.     stream_cursor_write w;
  130.  
  131.     if ( h != 0 )
  132.       {    /* Convert the unpacked data to concrete values in */
  133.         /* the source buffer. */
  134.         uint row_size = pss->WidthIn * c * pss->sizeofPixelIn;
  135.         const byte *bdata = buffer + data_x * c * pss->sizeofPixelIn;
  136.  
  137.         if ( pss->sizeofPixelIn == 1 )
  138.           {    /* Easy case: 8-bit device color values. */
  139.             r.ptr = bdata - 1;
  140.           }
  141.         else
  142.           {    /* Messy case: concretize each sample. */
  143.             int bps = penum->bps;
  144.             int dc = penum->spp;
  145.             const byte *pdata = bdata;
  146.             frac *psrc = (frac *)penum->line;
  147.             gs_client_color cc;
  148.             int i;
  149.  
  150.             r.ptr = (byte *)psrc - 1;
  151.             for ( i = 0; i < pss->WidthIn; i++, psrc += c )
  152.               {    int j;
  153.                 if ( bps <= 8 )
  154.                   for ( j = 0; j < dc; ++pdata, ++j  )
  155.                     { decode_sample(*pdata, cc, j);
  156.                     }
  157.                 else        /* bps == 12 */
  158.                   for ( j = 0; j < dc; pdata += sizeof(frac), ++j  )
  159.                     { decode_frac(*(const frac *)pdata, cc, j);
  160.                     }
  161.                 (*pcs->type->concretize_color)(&cc, pcs, psrc,
  162.                                    pis);
  163.               }
  164.           }
  165.         r.limit = r.ptr + row_size;
  166.       }
  167.     else            /* h == 0 */
  168.       r.ptr = 0, r.limit = 0;
  169.  
  170.     /*
  171.      * Process input and/or collect output.
  172.      * By construction, the pixels are 1-for-1 with the device,
  173.      * but the Y coordinate might be inverted.
  174.      */
  175.  
  176.     {    int xo = penum->xyi.x;
  177.         int yo = penum->xyi.y;
  178.         int width = pss->WidthOut;
  179.         int dy;
  180.         const gs_color_space *pconcs = cs_concrete_space(pcs, pis);
  181.         int bpp = dev->color_info.depth;
  182.         uint raster = bitmap_raster(width * bpp);
  183.  
  184.         if ( penum->matrix.yy > 0 )
  185.           dy = 1;
  186.         else
  187.           dy = -1, yo--;
  188.         for ( ; ; )
  189.         {    int ry = yo + penum->line_xy * dy;
  190.             int x;
  191.             const frac *psrc;
  192.             gx_device_color devc;
  193.             int code;
  194.             declare_line_accum(penum->line, bpp, xo);
  195.         
  196.             w.limit = penum->line + width * c *
  197.               sizeof(gx_color_index) - 1;
  198.             w.ptr = w.limit - width * c *
  199.               (sizeof(gx_color_index) - pss->sizeofPixelOut);
  200.             psrc = (const frac *)(w.ptr + 1);
  201.             code = (*s_IScale_template.process)
  202.               ((stream_state *)pss, &r, &w, false);
  203.             if ( code < 0 && code != EOFC )
  204.               return_error(gs_error_ioerror);
  205.             if ( w.ptr == w.limit )
  206.               { for ( x = xo; x < xo + width; x++, psrc += c )
  207.                   { (*pconcs->type->remap_concrete_color)
  208.                   (psrc, &devc, pis, dev,
  209.                    gs_color_select_source);
  210.                 if ( color_is_pure(&devc) )
  211.                   { /* Just pack colors into a scan line. */
  212.                     gx_color_index color = devc.colors.pure;
  213.                     line_accum(color, bpp);
  214.                   }
  215.                 else
  216.                   { line_accum_copy(dev, penum->line, bpp,
  217.                             xo, x, raster, ry);
  218.                     code = gx_fill_rectangle_device_rop(x, ry,
  219.                         1, 1, &devc, dev, lop);
  220.                     if ( code < 0 )
  221.                       return code;
  222.                     if ( bpp < 8 )
  223.                       { if ( (l_shift -= bpp) < 0 )
  224.                       *l_dst++ = (byte)l_bits, l_bits = 0,
  225.                       l_shift += 8;
  226.                       }
  227.                     else
  228.                       l_dst += bpp >> 3;
  229.                     l_xprev = x + 1;
  230.                   }
  231.                   }
  232.                 line_accum_copy(dev, penum->line, bpp,
  233.                         xo, x, raster, ry);
  234.                 penum->line_xy++;
  235.                 continue;
  236.               }
  237.             if ( r.ptr == r.limit || code == EOFC )
  238.               break;
  239.         }
  240.     }
  241.  
  242.     return (h == 0 ? 0 : 1);
  243. }
  244.